/**
 * 
 */
package com.armail.client.view.javafx;

import java.util.Collection;
import java.util.LinkedHashSet;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

import com.armail.client.view.NavigationView;
import com.armail.client.view.NavigationViewListener;
import com.armail.client.view.ViewLocation;
import com.armail.client.view.ViewManager;
import com.google.inject.Inject;

/**
 * @author arpit
 *
 */
public class FXNavigationView extends BorderPane implements NavigationView{
	protected final Collection<NavigationViewListener> listeners = new LinkedHashSet<NavigationViewListener>();
	private ViewManager viewManager;
	private final Button composeButton = new Button("Compose");
	private final Button inboxButton = new Button("Inbox");
	private final Button sentItemButton = new Button("Sent Items");
	private final Button draftsButton = new Button("Drafts");

	@Inject
	public FXNavigationView(ViewManager viewManager) {
		super();
		this.viewManager = viewManager;
	}
	
	@Override
	public void attachChild(Object subView, ViewLocation location) {
		// Only support (non-null) JavaFX Nodes
		if (!(subView instanceof Node)) {
			throw new IllegalArgumentException("subView must be an instance of " + Node.class.getName());
		}
		switch (location) {
		case COMPOSE_MAIL:
			setCenter((Node) subView);
			break;
		case INBOX:
			setCenter((Node) subView);
			break;
		case SENT_ITEMS:
			setCenter((Node) subView);
			break;
		case DRAFTS:
			setCenter((Node) subView);
			break;
		default:
			throw new IllegalArgumentException("location " + location + " is not supported");
		}
	}

	@Override
	public void detachChild(Object subView) {
		if (getCenter() == subView) {
			setCenter(null);
		} else if (getBottom() == subView) {
			setBottom(null);
		}
	}

	@Override
	public void attach() {
		init();
		viewManager.attach(this, ViewLocation.NAVIGATION);
		viewManager.registerLocations(this, ViewLocation.COMPOSE_MAIL, ViewLocation.INBOX, ViewLocation.SENT_ITEMS, ViewLocation.DRAFTS);
	}

	private void init() {
		StackPane topStackPane = new StackPane();
		topStackPane.setPrefHeight(80);
		topStackPane.setStyle("-fx-border-color:green");
		topStackPane.setPadding(new Insets(5));
		setTop(topStackPane);
		setLeft(getMenu());
	}

	private Node getMenu() {
		VBox vbox = new VBox();
		vbox.setPrefWidth(120);
		vbox.setSpacing(10);
		vbox.setPadding(new Insets(5));
		vbox.setStyle("-fx-border-color:red");
		composeButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				for (NavigationViewListener l : listeners) {
					l.onClickCompose();
				}
			}
		});
		
		inboxButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				for (NavigationViewListener l : listeners) {
					l.onClickInbox();
				}
			}
		});
		
		sentItemButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				for (NavigationViewListener l : listeners) {
					l.onClickSentItems();
				}
			}
		});
		
		draftsButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				for (NavigationViewListener l : listeners) {
					l.onClickDrafts();
				}
			}
		});
		
		vbox.getChildren().addAll(composeButton, inboxButton, sentItemButton, draftsButton);
		return vbox;
	}

	@Override
	public void detach() {
		viewManager.unregisterLocations(this);
		viewManager.detach(this, ViewLocation.NAVIGATION);
	}

	@Override
	public void done() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addListener(NavigationViewListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void removeListener(NavigationViewListener listener) {
		this.listeners.remove(listener);
	}
	
}
